﻿@namespace BootstrapBlazor.Components
@typeparam TModel
@inherits BootstrapComponentBase

<div @attributes="AdditionalAttributes" class="@ClassString">
    @if (Model != null)
    {
        <CascadingValue Value="@_editorItems" IsFixed="false">
            @FieldItems?.Invoke(Model)
        </CascadingValue>
        <CascadingValue Value="this" Name="EditorForm">
            <div class="form-body">
                @if (ShowUnsetGroupItemsOnTop)
                {
                    if (UnsetGroupItems.Any())
                    {
                        @RenderUnsetGroupItems
                    }
                    @foreach (var g in GroupItems)
                    {
                        @RenderGroupItems(g)
                    }
                }
                else
                {
                    @foreach (var g in GroupItems)
                    {
                        @RenderGroupItems(g)
                    }
                    if (UnsetGroupItems.Any())
                    {
                        @RenderUnsetGroupItems
                    }
                }
            </div>

            @if (Buttons != null)
            {
                <div class="bb-editor-footer form-footer">
                    @Buttons
                </div>
            }
        </CascadingValue>
    }
</div>

@code
{
    RenderFragment RenderUnsetGroupItems =>
    @<div class="@FormClassString" style="@FormStyleString">
        @foreach (var item in UnsetGroupItems)
        {
            var render = GetRenderTemplate(item);
            @if (render != null)
            {
                @render(Model)
            }
            else
            {
                <div class="@GetCssString(item)">
                    @AutoGenerateTemplate(item)
                </div>
            }
        }
    </div>;

    RenderFragment<KeyValuePair<string, IOrderedEnumerable<IEditorItem>>> RenderGroupItems => g =>
    @<GroupBox Title="@g.Key">
        <div class="@FormClassString" style="@FormStyleString">
            @foreach (var item in g.Value)
            {
                var render = GetRenderTemplate(item);
                @if (render != null)
                {
                    @render(Model)
                }
                else
                {
                    <div class="@GetCssString(item)">
                        @AutoGenerateTemplate(item)
                    </div>
                }
            }
        </div>
    </GroupBox>;
}
